package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.magicborrow.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created by wcx on 2016/1/22.
 */
public class GoPersonInfoActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvContent;
    private TextView tvComplete;
    private EditText etContent;
    private String content;
    private int cou = 0;
    private int selectionEnd = 0;
    private int mMaxLenth=25;
    private int type;
    private EditText etContentBottom;
    private String contentBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gopersonal);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        tvContent = (TextView) findViewById(R.id.tvcenter);
        tvComplete = (TextView) findViewById(R.id.confirm);
        etContent = (EditText) findViewById(R.id.et_content);
        etContentBottom = (EditText) findViewById(R.id.et_content_bottom);
    }

    private void initData() {
        type = getIntent().getIntExtra("type", 0);
        if (type ==0){
            tvContent.setText("名字");
            content = getIntent().getStringExtra(EditInfoActivity.TYPE_MODIFY);
            etContent.setHint("请填写名字");
            if(content.length()>10){
                content=content.substring(0,10);
            }
            etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
        }else if (type ==1){
            tvContent.setText("电话号码");
            content = getIntent().getStringExtra(EditInfoActivity.TYPE_MODIFY);
            etContent.setHint("请填写电话号码");
            etContent.setInputType(InputType.TYPE_CLASS_NUMBER);
        }else if(type==2){
            tvContent.setText("收货地址");
            findViewById(R.id.ll_bottom).setVisibility(View.VISIBLE);
            Bundle bundle=getIntent().getExtras();
            content=bundle.getString("address1");
            contentBottom = bundle.getString("address2");
            etContent.setHint("请填写地址");
            etContentBottom.setHint("请填写详细地址");
        }else if (type==3){
            tvContent.setText("个性签名");
            content = getIntent().getStringExtra(EditInfoActivity.TYPE_MODIFY);
            etContent.setHint("请填写个性签名");
            if(content.length()>25){
                content=content.substring(0,25);
            }
            etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
        }
        etContent.setText(content);
        etContent.setSelection(content.length());
        if (type==2){
            etContentBottom.setText(contentBottom);
            etContentBottom.setSelection(contentBottom.length());
        }
    }

    private void initListener() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cou = before + count;
                String editable = etContent.getText().toString();
                String str = stringFilter(editable);
                if (!editable.equals(str)) {
                    etContent.setText(str);
                }
                etContent.setSelection(etContent.length());
                cou = etContent.length();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (cou > mMaxLenth) {
                    selectionEnd = etContent.getSelectionEnd();
                    s.delete(mMaxLenth, selectionEnd);
                }
            }
        });

        etContentBottom.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cou = before + count;
                String editable = etContentBottom.getText().toString();
                String str = stringFilter(editable);
                if (!editable.equals(str)) {
                    etContentBottom.setText(str);
                }
                etContentBottom.setSelection(etContentBottom.length());
                cou = etContentBottom.length();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (cou > mMaxLenth) {
                    selectionEnd = etContentBottom.getSelectionEnd();
                    s.delete(mMaxLenth, selectionEnd);
                }
            }
        });

        tvComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type==2){
                    compeleteOther();
                }else{
                    compelete();
                }
            }
        });
    }


    private void compeleteOther() {
        String content1 = etContent.getText().toString();
        String content2 = etContentBottom.getText().toString();
        content1=content1.replaceAll(" ","");
        if("".equals(content1)){
            Toast.makeText(this,"需要填写内容",Toast.LENGTH_SHORT).show();
            return;
        }
        content2=content2.replaceAll(" ","");
        if("".equals(content2)){
            Toast.makeText(this,"需要填写内容",Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent =new Intent();
        intent.putExtra("content",content1+"#"+content2);
        setResult(type,intent);
        finish();
    }

    private void compelete() {
        String contentModify = etContent.getText().toString();
        contentModify=contentModify.replaceAll(" ","");
        if("".equals(contentModify)){
            Toast.makeText(this,"需要填写内容",Toast.LENGTH_SHORT).show();
            return;
        }

        if (type==1){
            if(etContent.getText().length()!=11){
                Toast.makeText(this,"请填写正确的电话格式",Toast.LENGTH_SHORT).show();
                return;
            }
        }

        Intent intent =new Intent();
        intent.putExtra("content",contentModify);
        setResult(type,intent);
        finish();
    }

    public  String stringFilter(String str)throws PatternSyntaxException {
        String regEx = "[/\\:*?<>|\"\n\t]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
